package com.woniu108.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.ProductItemDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.form.FindAllNormalProductItemForm;
import com.woniu108.product.form.FindAllProductItemForm;
import com.woniu108.product.form.InsertProductItemForm;
import com.woniu108.product.form.UpdateProductItemForm;
import com.woniu108.product.param.*;
import com.woniu108.product.service.ProductItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品总览 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/productItem")
@Api(tags = "商品总览接口")
public class ProductItemController {
    @Resource
    private ProductItemService productItemService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询所有商品总览
     *
     * @param form 商品种类编号,商品品牌编号
     * @return 商品总览详情
     */
    @PostMapping("/findAllProductItem.c")
    @ApiOperation("查询所有商品总览")
    public Result<List<ProductItemDto>> findAllProductItem(@RequestBody FindAllProductItemForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        FindAllProductItemParam param = BeanUtil.toBean(form, FindAllProductItemParam.class);
        param.setAdminId(adminId);
        List<ProductItemDto> dtos = productItemService.findAllProductItem(param);
        return Result.newListInstance(ProductItemDto.class).setData(dtos).setMsg("查询所有商品总览成功");
    }

    /**
     * 查询所有正常商品总览
     *
     * @param form 商品种类编号,商品品牌编号
     * @return 商品总览详情
     */
    @PostMapping("/findAllNormalProductItem")
    @ApiOperation("查询所有正常商品总览")
    public Result<List<ProductItemDto>> findAllNormalProductItem(@RequestBody FindAllNormalProductItemForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        FindAllNormalProductItemParam param = BeanUtil.toBean(form, FindAllNormalProductItemParam.class);
        param.setUserId(userId);
        List<ProductItemDto> dtos = productItemService.findAllNormalProductItem(param);
        return Result.newListInstance(ProductItemDto.class).setData(dtos).setMsg("查询所有正常商品总览成功");
    }

    /**
     * 新增商品总览
     *
     * @param form 商品名称,商品种类编号,,商品品牌编号
     * @return boolean值
     */
    @PostMapping("/insertProductItem.api")
    @ApiOperation("新增商品总览")@AdminHistoryLog
    public Result<Boolean> insertProductItem(@RequestBody InsertProductItemForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertProductItemParam param = BeanUtil.toBean(form, InsertProductItemParam.class);
        param.setAdminId(adminId);
        Boolean bool = productItemService.insertProductItem(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增商品总览成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增商品总览失败").setData(false).setCode(500);
    }

    /**
     * 修改商品总览名
     *
     * @param form 商品编号,新商品名称
     * @return boolean值
     */
    @PostMapping("/updateProductItem.api")
    @ApiOperation("修改商品总览名")@AdminHistoryLog
    public Result<Boolean> updateProductItem(@RequestBody UpdateProductItemForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateProductItemParam param = BeanUtil.toBean(form, UpdateProductItemParam.class);
        param.setAdminId(adminId);
        Boolean bool = productItemService.updateProductItem(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改商品总览名成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改商品总览名失败").setData(false).setCode(500);
    }

    /**
     * 禁用商品总览
     *
     * @param pItemIds 总览id集合
     * @return boolean值
     */
    @PostMapping("/banProductItem.api")
    @ApiOperation("禁用商品总览")@AdminHistoryLog
    @ApiImplicitParam(name = "pItemIds", value = "总览id集合")
    public Result<Boolean> banProductItem(@RequestBody List<Integer> pItemIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        BanProductItemParam param = new BanProductItemParam(pItemIds, adminId);
        Boolean bool = productItemService.banProductItem(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用商品总览成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用商品总览失败").setData(false).setCode(500);
    }

    /**
     * 解锁商品总览
     *
     * @param pItemIds 总览id集合
     * @return boolean值
     */
    @PostMapping("/unlockProductItem.api")
    @ApiOperation("解锁商品总览")@AdminHistoryLog
    @ApiImplicitParam(name = "pItemIds", value = "总览id集合")
    public Result<Boolean> unlockProductItem(@RequestBody List<Integer> pItemIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UnlockProductItemParam param = new UnlockProductItemParam(pItemIds, adminId);
        Boolean bool = productItemService.unlockProductItem(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁商品总览成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁商品总览失败").setData(false).setCode(500);
    }

    /**
     * 删除商品总览
     *
     * @param pItemIds 总览id集合
     * @return boolean值
     */
    @PostMapping("/deleteProductItem.api")
    @ApiOperation("删除商品总览")@AdminHistoryLog
    @ApiImplicitParam(name = "pItemIds", value = "总览id集合")
    public Result<Boolean> deleteProductItem(@RequestBody List<Integer> pItemIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteProductItemParam param = new DeleteProductItemParam(pItemIds, adminId);
        Boolean bool = productItemService.deleteProductItem(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除商品总览成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除商品总览失败").setData(false).setCode(500);
    }

    /**
     * 销量增加
     *
     * @param pItemId 总览id
     * @return boolean值
     */
    @GetMapping("/addPayNum")
    @ApiOperation("销量增加")
    @ApiImplicitParam(name = "pItemId", value = "总览id")
    public Result<Boolean> addPayNum(Integer pItemId) {
        Boolean bool = productItemService.addPayNum(pItemId);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("订单支付成功,商品销量增加成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("订单支付成功,商品销量增加失败").setData(false).setCode(500);

    }

    /**
     * 销量减少
     *
     * @param pItemId 总览id
     * @return boolean值
     */
    @GetMapping("/reducePayNum")
    @ApiOperation("销量减少")
    @ApiImplicitParam(name = "pItemId", value = "总览id")
    public Result<Boolean> reducePayNum(Integer pItemId) {
        Boolean bool = productItemService.reducePayNum(pItemId);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("退款,商品销量减少成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("退款,商品销量减少失败").setData(false).setCode(500);
    }

    /**
     * 通过id查商品总览
     * @param pItemId 总览id
     * @return 商品总览详情
     */
    @GetMapping("/findProductItemByItemId")
    @ApiOperation("通过id查商品总览")
    @ApiImplicitParam(name = "pItemId", value = "总览id")
    public Result<ProductItemDto> findProductItemByItemId(Integer pItemId) {
        ProductItemDto dto = productItemService.findProductItemByItemId(pItemId);
        return Result.newInstance(ProductItemDto.class).setData(dto).setMsg("查询所有商品总览成功");

    }
}

